﻿/********************************************************************************************
 * 文件名称:	User.cs
 * 设计人员:	严(yanxianghui@gmail.com)
 * 设计时间:	2009-03-16
 * 功能描述:	管理员
 * 注意事项:	
 * 版权所有:	Copyright (c) 2009, Fujian SIRC
 * 修改记录: 	修改时间		人员		修改备注
 *				    ----------		------		-------------------------------------------------
 *
 ********************************************************************************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Shanfree.Framework.Utility;
using Shanfree.SharpMovie.Model;
using Shanfree.SharpMovie.IDAL;

namespace Shanfree.SharpMovie.SQLServerDAL
{
    /// <summary>
    /// 管理员
    /// </summary>
    public class User : DBPersistenceBase<UserInfo>, IUser
    {
        #region SQL
        private const string SQL_SELECT_ALL = @"SELECT *
                                                                              FROM [dbo].[User]";

        private const string SQL_SELECT_NAME = @"SELECT top 1 *
                                                                              FROM [dbo].[User]
                                                                              WHERE Name = @Name";

        private const string SQL_SELECT_FRIENDS = @"SELECT U.*
                                                                                    FROM [dbo].[User] U,dbo.UserRelation UR
                                                                                    WHERE U.ID = UR.UserBID
                                                                                    AND UR.UserAID = @UserID";

        private const string SQL_SELECT_FOLLOWERS = @"SELECT U.*
                                                                                    FROM [dbo].[User] U,dbo.UserRelation UR
                                                                                    WHERE U.ID = UR.UserAID
                                                                                    AND UR.UserBID = @UserID";

        private const string SQL_SELECT_PROFILE = @"
                                                                                select Us.* ,
                                                                                (
                                                                                    select count(1) 
                                                                                    from 
                                                                                    dbo.[User] U,
                                                                                    dbo.UserRelation UR
                                                                                    where 
                                                                                    UR.UserBID = U.ID
                                                                                    AND UR.UserAID = Us.ID
                                                                                ) AS Friends,
                                                                                (
                                                                                    select count(1) 
                                                                                    from 
                                                                                    dbo.[User] U,
                                                                                    dbo.UserRelation UR
                                                                                    where 
                                                                                    UR.UserAID = U.ID
                                                                                    AND UR.UserBID = Us.ID
                                                                                ) AS Followers,
                                                                                (
                                                                                    select count(1) 
                                                                                    from 
                                                                                    dbo.Comment C
                                                                                    where 
                                                                                    C.UserID = Us.ID
                                                                                ) AS Comments
                                                                                from dbo.[User] Us
                                                                                where Us.ID = @UserID";

        #endregion

        protected override string ConnectionString
        {
            get
            {
                return DBConnection.ConnectionString;
            }
        }

        /// <summary>
        /// 根据列表
        /// </summary>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <returns></returns>
        public IList<UserInfo> GetList(int pageSize, int pageIndex)
        {
            string orderSQL = CommonHelper.GetPagedSQL(SQL_SELECT_ALL, pageSize, pageIndex);
            SqlDataReader reader = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.Text, orderSQL);
            IList<UserInfo> list = GetListFromReader(reader);
            CustomList<UserInfo> cList = PageAmoutHelper<UserInfo>.GetCutomList(this.ConnectionString, orderSQL, pageSize, list);
            return cList;
        }
        /// <summary>
        /// 获取评论
        /// </summary>
        /// <param name="name">name</param>
        /// <returns></returns>
        public UserInfo GetByName(string name)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@Name", name);
            SqlDataReader reader = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.Text, SQL_SELECT_NAME, param);
            IList<UserInfo> list = GetListFromReader(reader);
            if (list.Count == 0)
            {
                return null;
            }
            return list[0];
        }

        /// <summary>
        /// 获取好友列表
        /// </summary>
        /// <param name="ID">对象主键</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <returns>对象实体</returns>
        public IList<UserInfo> GetFriendList(string userID, int pageSize, int pageIndex)
        {
            string orderSQL = CommonHelper.GetPagedSQL(SQL_SELECT_FRIENDS, pageSize, pageIndex);
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@UserID", userID);
            SqlDataReader reader = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.Text, orderSQL, param);
            IList<UserInfo> list = GetListFromReader(reader);
            CustomList<UserInfo> cList = PageAmoutHelper<UserInfo>.GetCutomList(this.ConnectionString, orderSQL, param, pageSize, list);
            return cList;
        }

        /// <summary>
        /// 获取用户的关注者列表
        /// </summary>
        /// <param name="ID">对象主键</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <returns>对象实体</returns>
        public IList<UserInfo> GetFollowerList(string userID, int pageSize, int pageIndex)
        {
            string orderSQL = CommonHelper.GetPagedSQL(SQL_SELECT_FOLLOWERS, pageSize, pageIndex);
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@UserID", userID);
            SqlDataReader reader = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.Text, orderSQL, param);
            IList<UserInfo> list = GetListFromReader(reader);
            CustomList<UserInfo> cList = PageAmoutHelper<UserInfo>.GetCutomList(this.ConnectionString, orderSQL, param, pageSize, list);
            return cList;
        }

        #region 用户个人信息
        /// <summary>
        /// 用户个人信息
        /// </summary>
        /// <param name="userID">用户主键</param>
        /// <returns>用户个人信息</returns>
        public UserInfo GetUserProfile(string userID)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@UserID", userID);
            SqlDataReader reader = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.Text, SQL_SELECT_PROFILE, param);
            IList<UserInfo> list = GetListFromReader(reader, WriteProfileExtendProperty);
            if (list.Count == 0)
            {
                return null;
            }
            return list[0];
        }

        /// <summary>
        /// 写入扩展属性
        /// </summary>
        /// <param name="reader">数据库游标</param>
        /// <param name="t">实体对象</param>
        /// <returns></returns>
        private UserInfo WriteProfileExtendProperty(SqlDataReader reader, UserInfo uInfo)
        {
            uInfo.Friends = Convert.ToString(reader["Friends"]);
            uInfo.Followers = Convert.ToString(reader["Followers"]);
            uInfo.Comments = Convert.ToString(reader["Comments"]);
            return uInfo;
        }

        #endregion
    }
}
